﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using LocalService;
using Models;
using OnTheWay.Enums;
using System.Collections.ObjectModel;

namespace PcBackgroundMgr.Functions
{
    /// <summary>
    /// ExhibitSelectCtrl.xaml 的交互逻辑
    /// </summary>
    public partial class ExhibitSelectCtrl : ExhibitModelCtrl
    {
        //private Exhibit mDefault = new Exhibit();

        public ExhibitListCtrl ExhibitList { get; set; }

        public ExhibitSelectCtrl()
        {
            this.Title = "选择相关内容";
            ExhibitList = new ExhibitListCtrl();
            InitializeComponent();
        }

        protected override void OnInitialized(EventArgs e)
        {
            base.OnInitialized(e);
            if (fnItems == null)
            {
                ExhibitList.CommandRefresh.Execute(null);
            }
            else
            {
                WaitVisibility = Visibility.Visible;
                ExhibitList.Items = new ObservableCollection<Exhibit>(fnItems);
                ExhibitList.PageSize = fnPageSize;
                ExhibitList.PageIndex = fnPageIndex;
                //ExhibitList.PageCount = fnPageCount;
                ExhibitList.TotalCount = fnTotalCount;
                //ExhibitList.ViewStart = fnViewStart;
                //ExhibitList.ViewEnd = fnViewEnd;
                WaitVisibility = Visibility.Collapsed;
            }
        }

        protected override bool OnCanSave(Exhibit model)
        {
            if (model == null)
            {
                Message = "您还没有选择条目";
                MessageStatus = MessageStatusEnum.Warning;
            }
            return model != null;
        }

        protected override void OnTaskAdding(Exhibit model)
        {
            //base.OnTaskAdding(model);
        }

        protected override void OnTaskModifying(Exhibit model)
        {
            //base.OnTaskModifying(model);
        }

        private void ExhibitItemCtrl_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            this.OnCommandSave(null);
        }

        protected override void OnModelAdded(Exhibit model)
        {
            base.OnModelAdded(model);
            DispatcherInvokeSafety(() =>
            {
                OnCommandClose(null);
            });
        }

        private static int fnPageSize = -1;
        private static int fnPageIndex = -1;
        private static int fnPageCount = -1;
        private static int fnViewStart = -1;
        private static int fnViewEnd = -1;
        private static long fnTotalCount = -1;
        private static IEnumerable<Exhibit> fnItems = null;
        public override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);
            fnPageSize = ExhibitList.PageSize;
            fnPageIndex = ExhibitList.PageIndex;
            fnPageCount = ExhibitList.PageCount;
            fnTotalCount = ExhibitList.TotalCount;
            fnViewStart = ExhibitList.ViewStart;
            fnViewEnd = ExhibitList.ViewEnd;
            fnItems = ExhibitList.Items;
        }
    }
}
